package algorithm.swordoff;

import algorithm.leetcode.ListNode;

import java.util.Arrays;

/**
 * 从尾到头打印链表
 *
 * 反转链表还是不太熟
 * 反转链表需要创建三个变量,while中是四行代码
 */

public class SQ06 {

    public int[] reversePrint(ListNode head) {
        ListNode curr = head;
        ListNode prev = null;
        ListNode post;
        int num = 0;

        // 先反转链表
        while (curr != null) {
            post = curr.next;
            curr.next = prev;
            prev = curr;
            curr = post;
            num ++;
        }

        int[] ints = new int[num];
        num = 0;
        // 填充数组
        while (prev != null) {
            ints[num++] = prev.val;
            prev = prev.next;
        }
        return ints;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(4);
        ListNode node3 = new ListNode(9);
        // node1.next = node2;
        // node2.next = node3;
        SQ06 sq06 = new SQ06();
        System.out.println(Arrays.toString(sq06.reversePrint(null)));

    }

}
